<template>
 
  <el-main>
    <el-row :gutter="1">
    <el-col :span="12">
    <div class="grid-content bg-purple">
          <div slot="header" class="clearfix">
      
            <!-- 头像 -->
            <el-avatar :size="42" :src=" secondHand.user.headImg" style="float: left;"></el-avatar>
            <!-- 用户信息 -->
            <span style="position: relative;left: 8px;">
              <!-- 昵称 -->
              <span>{{ secondHand.user.nickname}}</span>
              <!-- 如果是当前用户发布的 -->
              <span v-if="user.id== secondHand.sellUid" style="color:rgb(144,147,153);font-size:14px;">（个人发布）</span>
              <span v-else style="color:rgb(144,147,153);font-size:14px;">（其它用户发布）</span>
              <!-- 发布时间和小区信息 -->
              <span style="color:rgb(144,147,153);font-size:12px;">{{ secondHand.releaseTime}} </span>

            </span>

          </div>
           <el-divider></el-divider>

            <template>
              {{ secondHand.title}}
          </template>
          <div v-if=" secondHand.photos">
              <el-image v-for="(photo,p_i) in secondHand.photos.split(',')" :key="p_i"
                style="width: 300px; height: 150px;margin-left: 2px;margin-right: 2px;"
                :src="photo"
                :preview-src-list=" secondHand.photos.split(',')">
              </el-image>
          </div>
          <div>商品介绍：{{ secondHand.introduce}}</div>
          <div>商品数量：{{secondHand.store}}</div>
          <div>价格：{{ secondHand.price}}</div>

          <!-- 订单对话框 -->
          <span v-if="user.id!= secondHand.sellUid& secondHand.store!=0" style="color:rgb(144,147,153);font-size:14px;">
            <el-button><i class="el-icon-sell" @click="wantBuy(index)">可购买</i></el-button>
              </span>
            <span v-else style="color:rgb(144,147,153);font-size:14px;">
            <i class="el-icon-sell" >不可购买</i>
            </span>
           <el-button type="text" @click="clickOk(index)">
            <span v-if=" secondHand.secondOk==1">
              <i class="el-icon-star-on"></i>
              取消点赞({{ secondHand.okSum}})
            </span>
            <span v-else>
              <i class="el-icon-star-off"></i>
              点赞({{ secondHand.okSum}})
            </span>
          </el-button>

           <el-popover
            placement="right"
            title="评论详情"
            width="400"
            trigger="click">
            <!-- 评论按钮 -->
            <el-button slot="reference" type="text">
              <i class="el-icon-chat-line-square"></i>
              评论({{ secondHand.views}})
            </el-button>

            <el-input v-model="comment1" placeholder="说点什么？" size="small" style="width:350px;"></el-input>
            <el-button type="primary" icon="el-icon-edit" size="small" plain @click="comment(index)"></el-button>

            <!-- 评论for遍历展示 -->
            <div v-for="(comment, i) in  secondHand.secondHandComments" :key="i" style="margin-top:10px;border-bottom:1px solid rgb(230,230,230);">
              <el-avatar :size="40" :src="comment.user.headImg" style="float: left;"></el-avatar>
              <!-- 用户信息 -->
              <div style="position: relative;left: 50px;">
                <!-- 昵称和正文 -->
                <span style="position: relative;right: 40px;">
                  {{comment.user.nickname}}
                  <br>
                  <span style="font-size:16px;">
                    {{comment.content}}
                  </span>
                  <br>
                </span>
                <!-- 发布时间 -->
                <span style="color:rgb(144,147,153);font-size:12px;">
                  {{comment.releaseTime}}
                </span>
              </div>
            </div>
      
           </el-popover>
        </div>
    </el-col>
    </el-row>


      <el-dialog
        title="请填写订单信息"
        :visible.sync="dialogVisible"
        center
        width="50%">
            <div style="text-align:center;">{{orderHand.title}}</div>
            <div></div>


            <!-- 商品数量 -->
            <el-form :model="newOrder" ref="newOrder" label-width="100px" class="demo-ruleForm">
              <el-form-item
                label="商品数量"
                prop="num"
                :rules="[
                  { required: true, message: '商品数量为空'},
                  { type: 'number', message: '商品数量为数字值'}
                ]"
              >
                <el-input type="text" v-model.number="newOrder.num" autocomplete="off"></el-input>
              </el-form-item>
                <!-- 添加地址 -->
            <div>请选择地址</div>
            <div style="text-align:center;">
                <template >
              <el-table
                ref="singleTable"
                :data="address"
                align="center"
                highlight-current-row
                @current-change="handChange"
                style="width: 70%">


                <el-table-column
                  property="name"
                  label="姓名"
                  width="120">
                </el-table-column>
                <el-table-column
                  property="phone"
                  label="手机号"
                  width="120">
                </el-table-column>
                <el-table-column
                  property="address"
                  label="地址">
                </el-table-column>
              </el-table>
            </template>
            </div>
              <el-form-item>
                <el-button @click="dialogVisible = false" >取 消 订 单</el-button>
                <el-button type="primary" @click="submitForm('newOrder')">生 成 订 单</el-button>

              </el-form-item>

            </el-form>

      </el-dialog>
    </el-main>

</template>

<script>
  export default {
    data() {
      return {
        comment1:'',
        secondHand:JSON.parse(this.$route.query.str),
        address:{},
         delivery: false,
         orderHand:{},
          newOrder:{
            num: 0,
            gid:0
           },
           currentRow: '',
        user:'',
        dialogVisible:false
      }
    },
    methods: {
        clickOk:function(index){
        console.log("http://localhost:8080/second/ok/" + this.secondHand.id);
        this.$axios.get("http://localhost:8080/second/ok/" + this.secondHand.id).then(res => {
          console.log(res);
          if(res.data.status == "SUCCESS"){
            this.secondHand.okSum ++
            this.secondHand.secondOk = 1
            return
          }
          if(res.data.status == "FAIL"){
            this.secondHand.okSum --
            this.secondHand.secondOk = 0
          }else{
            this.$message.error("操作失败")
          }
      })
      },
      comment:function(index){
         if(this.comment1==""||this.comment1==null){
          this.$message.error("不可以评论空的内容哦~")
          console.log("评论为空");
          return
        }
       this.$axios.get("http://localhost:8080/second/addComment/"+this.secondHand.id+"/"+this.comment1).then(res=>{
        if(res.data.status= "SUCCESS"){
            this.$message.success("评论成功，刷新后可见")
            /* let length =this.secondHand.secondHandComments.length
            this.information.data.views++ */
         /*  console.log(res.data.data); */

            /* console.log("前"+JSON.stringify(this.secondHand.secondHandComments));
             */
            /* this.secondHand.secondHandComments.splice(length,0,res.data.data) */
            /* console.log("后"+JSON.stringify(this.secondHand.secondHandComments)) */
          }else{
            this.$message.error("评论失败")
          }
       })

      },
        wantBuy:function(index){
          this.newOrder.num=''
          this.currentRow=''
          this.orderHand= this.secondHand
          this.newOrder.gid=this.secondHand.id
          this.$axios.get("http://localhost:8080/address/find").then(res=>{

          if(res.data.status= "SUCCESS"){
            console.log(res.data);
            this.address=res.data.data
            this.dialogVisible=true
           
            
          }else{
            alert("地址获取失败")
          }

        })
        this.orderHand=this.secondHand


      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.newOrder.num<1){
               this.$message.error("数量太小")
               return false;
            }
            if(this.newOrder.num>this.orderHand.store){
              this.$message.error("数量太大")
              return false;
            }

          
            if(this.newOrder.gid!=''&this.currentRow!=''){
               this.$axios.post("http://localhost:8080/second/addSecondOrder/"+this.newOrder.gid+"/"+this.newOrder.num+"/"+this.currentRow).then(res=>{

                  if(res.data.status= "SUCCESS"){
                     this.$message.success("订单生成成功")
                      this.$router.push( "/account/mySecondHandOrder");
                  }else{
                    this.$message.success("订单生成失败")
                  }

                  })

            }else{
              this.$message.error("请选择地址")
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },


      /* 地址相关方法 */
       setCurrent(row) {
        this.$refs.singleTable.setCurrentRow(row);
      },
      handChange:function(val){
          console.log(val.id);
          this.currentRow=val.id
      }

    },
    mounted() {
      /* 设置页面标题 */
      document.title="商品详情 - 汤臣一品便民服务中心"
      this.user=JSON.parse(sessionStorage.getItem('user'))
      console.log(JSON.parse(this.$route.query.str))
    }
  }



</script>

<style scoped>

</style>